<!-- 修改状态弹窗 -->
<template>
  <el-dialog v-model="dialogVisible" :title="`${dialogProps.title}`" width="500px" draggable>
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="dialogProps.isView"
      :model="dialogProps.row"
      :hide-required-asterisk="dialogProps.isView"
    >
      <el-form-item label="任务单状态" prop="taskStatus">
        <el-select v-model="dialogProps.row!.taskStatus" placeholder="请选择任务单状态" clearable>
          <el-option v-for="item in selectTaskStatus" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" v-show="!dialogProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="StatusDialog">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { selectTaskStatus } from "@/utils/serviceDict";
import { Substation } from "@/api/interface";

const rules = reactive({
  taskStatus: [{ required: true, message: "请选择任务状态" }]
});

interface DialogProps {
  title: string;
  isView: boolean;
  row: Partial<Substation.ResTaskOrderList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const dialogVisible = ref(false);

const dialogProps = ref<DialogProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DialogProps) => {
  dialogProps.value = params;
  dialogVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await dialogProps.value.api!({ taskId: dialogProps.value.row.taskId, taskStatus: dialogProps.value.row.taskStatus });
      ElMessage.success({ message: `修改状态成功` });
      dialogProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
